package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.MallGoodsSpec;
import com.fengmi.entity.MallGoodsSpecOptions;
import com.fengmi.mapper.GoodsSpecMapper;
import com.fengmi.service.GoodsSpecOptionsService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional//aop
public class GoodsSpecOptionsServiceImpl implements GoodsSpecOptionsService {

    @Autowired
    private GoodsSpecMapper specMapper;

    @Override
    public PageResultVO<MallGoodsSpecOptions> findSpecOptionsByPage(PageDTO<MallGoodsSpecOptions> pageDTO) {
        //非空判断
        if (pageDTO == null) {
            return new PageResultVO<>(false, "参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper层查询
        List<MallGoodsSpecOptions> specOptionsList = specMapper.findSpecOptionsByPage(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<MallGoodsSpecOptions> pageInfo = new PageInfo<>(specOptionsList);
        //创建返回对象
        PageResultVO<MallGoodsSpecOptions> pageResultVO = new PageResultVO<>(true, "查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO saveSpecAndSpecOptions(MallGoodsSpec mallGoodsSpec) {
        if (mallGoodsSpec == null) {
            return new ResultVO(false, "参数不合法");
        }
        //先查询规格名称是否存在
        MallGoodsSpec specBySpecName = specMapper.findMallGoodsSpecBySpecName(mallGoodsSpec.getSpecName());
        int count = 0;
        //判断是否存在该规格
        if (specBySpecName == null) {
            //没有该规格，则往数据库中插入，没有规格必定没有规格选项
            int i = specMapper.insertSpec(mallGoodsSpec);
            //添加规格选项
            for (MallGoodsSpecOptions specOption : mallGoodsSpec.getOptions()) {
                //设置添加的主键
                specOption.setSpecId(mallGoodsSpec.getId());
                int i1 = specMapper.insertSpecOptions(specOption);
                if (i1 > 0) {
                    count++;
                }
            }
            count += i;
            return new ResultVO(count > 1 ? true : false, count > 1 ? "添加成功" : "添加失败");
        }
        //已存在该规格，添加规格选项即可，通过规格选项名称查询是否存在
        for (MallGoodsSpecOptions specOption : mallGoodsSpec.getOptions()) {
            //查询数据库中该规格选项是否存在
            MallGoodsSpecOptions options = specMapper.findMallGoodsSpecOptionsByOptionName(specOption.getOptionName());
            //判断
            if (options == null) {
                //规格选项不存在，添加到数据库
                //设置已存在的规格的主键id
                specOption.setSpecId(specBySpecName.getId());
                //添加到数据库中
                int i = specMapper.insertSpecOptions(specOption);
                if (i > 0) {
                    count++;
                }
            }
        }
        return new ResultVO(count > 0 ? true : false, count > 0 ? "添加成功" : "添加失败");
    }

    @Override
    public ResultVO selectOneBySpecOptionsId(String id) {
        if (StringUtils.isEmpty(id)) {
            return new ResultVO(false, "参数不合法");
        }
        MallGoodsSpecOptions specOptions = specMapper.findOneById(id);
        if (specOptions == null) {
            return new ResultVO(false, "查询失败");
        }
        return new ResultVO(true, "查询成功", specOptions);
    }

    @Override
    public ResultVO updateOptions(MallGoodsSpecOptions mallGoodsSpecOptions) {
        //非空判断
        if (mallGoodsSpecOptions == null) {
            return new ResultVO(false, "参数不合法");
        }
        //调用mapper层修改规格选项信息
        int i = specMapper.updateSpecOptions(mallGoodsSpecOptions);
        //返回resultVo
        return new ResultVO(i == 1 ? true : false, i == 1 ? "修改规格选项成功" : "修改规格选项失败");
    }

    @Override
    public ResultVO deleteOneById(String id) {
        //非空判断
        if (StringUtils.isEmpty(id)) {
            return new ResultVO(false, "参数不合法");
        }
        int i = specMapper.deleteOptionsById(id);

        return new ResultVO(i == 1 ? true : false, i == 1 ? "删除规格选项成功" : "删除规格选项失败");
    }

    @Override
    public ResultVO selectAllSpec() {

        List<MallGoodsSpec> specList = specMapper.findAllSpec();

        return new ResultVO(true, "sucess", specList);
    }
}
